package com.offcn.member.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.Query;

import com.offcn.member.dao.UmsMemberDao;
import com.offcn.member.entity.UmsMemberEntity;
import com.offcn.member.service.UmsMemberService;

import javax.annotation.Resource;


@Service("umsMemberService")
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberDao, UmsMemberEntity> implements UmsMemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UmsMemberEntity> page = this.page(
                new Query<UmsMemberEntity>().getPage(params),
                new QueryWrapper<UmsMemberEntity>()
        );

        return new PageUtils(page);
    }


    //mybatis-plus自动生成的，有其自己的方法
    @Resource
    private UmsMemberDao umsMemberDao;

    /**
     * 用户数据统计
     * 返回值是一个List集合{xAxis{type时间,data},yAxis{type:'value'},series[对象{type,data就是y轴的值}]}
     * 所以可以看做List集合里面是一个个Map<type,data>
     *
     * @param beginTime
     * @param endTime
     */
    @Override
    public List<Map<String, Object>> countMember(String beginTime, String endTime) {
        //根据时间区间来查,传过来的时间需要格式化再查询
        QueryWrapper<UmsMemberEntity> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT(DATE_FORMAT(create_time,'%Y-%m-%d')) AS NAME,COUNT(id) AS VALUE")
                .between("create_time",beginTime+" 00:00:00",endTime+" 23:59:59").groupBy("NAME");
        //用mybatis-plus生成的方法来查询
        List<Map<String, Object>> mapList = umsMemberDao.selectMaps(wrapper);
        return mapList;
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     */
    @Override
    public UmsMemberEntity login(String username, String password) {
        QueryWrapper<UmsMemberEntity> queryWrapper = new QueryWrapper<UmsMemberEntity>()
                .eq("user_name", username)
                .eq("PASSWORD", password);
        UmsMemberEntity memberEntity = this.getOne(queryWrapper);
        if (null != memberEntity){  //null写前面防止空指针错误，若是空指针就不会往下走了
            return memberEntity;
        }
        return null;
    }

}